<template>
  <div id=messageListPage>
    <div style="text-align: left;padding-bottom: 10px">
      <el-button type="primary" @click="addMessage">添加通知</el-button>
    </div>

    <el-table :data="tableData" border style="width: 100%" v-loading ='loading'>
      <el-table-column prop="id" label="ID" width="80">
      </el-table-column>
      <el-table-column prop="title" label="标题">
      </el-table-column>
      <el-table-column prop="date" label="日期">
      </el-table-column>
      <el-table-column label="修改">
        <template slot-scope="scope">
           <el-button type ="text" @click='reviseMessage(scope.row.id,scope.row)'>修改</el-button>
        </template>
      </el-table-column>
      <el-table-column label="删除">
        <template slot-scope="scope">
          <el-button type ="text" @click="deleteMessage(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="text-align: right;padding-right: 40px;">

      <el-pagination :current-page.sync="pageNumber" @current-change='handleChange' :page="pageNumber" :page-size="pageSize" layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="title" :visible.sync="dialogVisible" size="tiny">
              <my-message :addOrRevise = 'status' :mesId = 'mesId' :mesDetail='mesDetail' @isSuccess='loadMore'></my-message>
    </el-dialog>
  </div>
</template>

<script>
  import message from './message.vue'
  import  dateformat from 'dateformat'
  export default {
    data () {
      return {
        dialogVisible: false,
        status:'',
        title:'',
        mesId:'',
        pageSize:10,
        pageNumber:1,
        total:1,
        tableData: [],
        loading:false,
        mesDetail:{}
      }
    },
    created(){
      this.getAllMes()
    },
    methods: {
      addMessage(){

        this.dialogVisible = true;
        this.status = 'add';
        this.title = '添加通知';
      },
      loadMore(val){
        this.dialogVisible = false;
        if(val == 'success'){
          this.getAllMes()
        }else{
          this.dialogVisible = false;
        }

      },
      reviseMessage(id,row){
        this.dialogVisible = true;
        this.status = 'revise';
        this.title = '修改通知';
        this.mesId = id;
        this.mesDetail = row;

      },
      deleteMessage(id){
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.deleteMessage(id).then(res =>{
            if(res.code == '000000'){
              console.log(res.data);
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getAllMes()
            }
          })

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

      },
      getAllMes() {
        this.loading = true;
        let params = {
          pageSize: this.pageSize,
          pageNumber: this.pageNumber
        }
        this.$api.getAllMes(params).then(res => {
          if (res.code == '000000') {
            console.log(res.data);
            this.loading = false;
            this.tableData = res.data.list;
            this.total = res.data.totalRow;
            this.tableData.forEach(item => {
              let date = new Date(item.date);
              item.date = dateformat(date, 'yyyy-mm-dd')
            })
          }
        })
      },
      handleChange(val){
        this.pageNumber = val;
        this.getAllMes()
      }
    },
    components:{
      'my-message' : message
    }


  }
</script>

<style lang="less">
  #messageListPage {
    th{
      text-align: center;
    }
  }
</style>
